﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;


namespace Hxsoft.AppV1._Controls
{
	public partial class LinkBrowser : Hxsoft.AppV1.Page
	{
		private LoadedValue<int> loadedLinkId;
		public int LinkId
		{
			get {
				if (loadedLinkId.Loader == null)
					loadedLinkId.Loader = () => int.Parse(Request.QueryString["InfoId"]);
				return loadedLinkId.Value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				this.DataBind();
		}

		protected override void Render(HtmlTextWriter writer)
		{
			string url = Module.Links.GetItem(LinkId).LinkUrl;
			if (!String.IsNullOrEmpty(url))
			{
				url =( url.Contains("http") || url.Trim().IndexOf('/')==0 )? url : string.Format(@"http://{0}",url);
				Response.Redirect(url);
			}
			else
			{
				Response.Write(string.Format(@"<script>window.alert(""{0}"");window.close();</script>",Res.LinkNote));
			}
		}
	}
}
